using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;

namespace 纺织贸易管理系统
{
    public static class DanjuTableService
    {
        public static List<DanjuTable> GetDanjuTablelst(Expression<Func<DanjuTable ,bool>> func)
         {
            return  Connect.dbcontext.Queryable<DanjuTable>().Where(func).OrderByDescending(x => x.rq).ToList();
         }
        public static List<DanjuTable> GetDanjuTablelst()
        {
            return Connect.CreatConnect().Query<DanjuTable>().OrderByDescending(x => x.rq).ToList();
        }
        public static DanjuTable GetOneDanjuTable(Expression<Func<DanjuTable, bool>> func)
         {
            return  Connect.dbcontext.Queryable<DanjuTable>().First(func)??new DanjuTable();
         }
        public static void InsertDanjuTablelst(List<DanjuTable> DanjuTableObjs)
         {
            Connect.dbcontext.Insertable<DanjuTable>(DanjuTableObjs).ExecuteCommand();
        }
        public static void InsertDanjuTable(DanjuTable DanjuTableObj)
        {
            Connect.dbcontext.Insertable<DanjuTable>(DanjuTableObj).ExecuteCommand ();
            RemainMoneyTableService.InsertRemainMoneyTable(new RemainMoneyTable()
            {
                DanJuJinE = DanjuTableObj.je,
                ReceiptNum = DanjuTableObj.dh,
                ShengYuMoney = DanjuTableObj.je,
                MachineType = DanjuTableObj.djlx,
                Rq = DanjuTableObj.rq,
                SupplierName = DanjuTableObj.ksmc,
                SupplierNum = DanjuTableObj.ksbh
            });
            if(DanjuTableObj.djlx==DanjuLeiXing.打样工艺单 )
            {
                RemainMoneyTableService.InsertRemainMoneyTable(new RemainMoneyTable()
                {
                    DanJuJinE = DanjuTableObj.je,
                    ReceiptNum = DanjuTableObj.dh,
                    ShengYuMoney = DanjuTableObj.je,
                    MachineType = DanjuTableObj.djlx,
                    Rq = DanjuTableObj.rq,
                    SupplierName = DanjuTableObj.SaleMan ,
                    SupplierNum = DanjuTableObj.wuliuBianhao 
                });
            }
        }
     
        public static void UpdateDanjuTable(string Updatestring, Expression<Func<DanjuTable, bool>> func)
        {
            Connect.CreatConnect().Update<DanjuTable>(Updatestring, func);
        }
        public static void UpdateDanjuTable(DanjuTable DanjuTableObj, Expression<Func<DanjuTable, bool>> func)
         {
            Connect.dbcontext.Updateable<DanjuTable>(DanjuTableObj).ExecuteCommand();
            //if (DanjuTableObj.djlx != DanjuLeiXing.报价单)
            //{
            //    if (!string.IsNullOrEmpty(DanjuTableObj.ksbh))
            //    {
            //        var remain = RemainMoneyTableService.GetOneRemainMoneyTable(x => x.ReceiptNum == DanjuTableObj.dh && x.SupplierNum == DanjuTableObj.ksbh);
            //        Connect.dbcontext.Updateable<RemainMoneyTable>().UpdateColumns(x => x.DanJuJinE == DanjuTableObj.je &&
            //           x.ReceiptNum == DanjuTableObj.dh &&
            //             x.ShengYuMoney == DanjuTableObj.je - (remain.DanJuJinE - remain.ShengYuMoney) &&
            //            x.MachineType == DanjuTableObj.djlx &&
            //             x.Rq == DanjuTableObj.rq &&
            //            x.SupplierName == DanjuTableObj.ksmc &&
            //            x.SupplierNum == DanjuTableObj.ksbh).Where(x => x.ReceiptNum == DanjuTableObj.dh && x.SupplierNum == DanjuTableObj.ksbh);
            //    }
            //}
            //if (DanjuTableObj.djlx == DanjuLeiXing.打样工艺单)
            //{
            //    if (!string.IsNullOrEmpty(DanjuTableObj.ksbh))
            //    {
            //        var remaikehu = RemainMoneyTableService.GetOneRemainMoneyTable(x => x.ReceiptNum == DanjuTableObj.dh && x.SupplierNum == DanjuTableObj.ksbh);
            //        Connect.dbcontext.Updateable<RemainMoneyTable>().UpdateColumns(x => x.DanJuJinE == DanjuTableObj.je &&
            //         x.ReceiptNum == DanjuTableObj.dh &&
            //           x.ShengYuMoney == DanjuTableObj.je - (remaikehu.DanJuJinE - remaikehu.ShengYuMoney) &&
            //          x.MachineType == DanjuTableObj.djlx &&
            //           x.Rq == DanjuTableObj.rq &&
            //          x.SupplierName == DanjuTableObj.ksmc &&
            //          x.SupplierNum == DanjuTableObj.ksbh).Where(x => x.ReceiptNum == DanjuTableObj.dh && x.SupplierNum == DanjuTableObj.wuliuBianhao);
            //    }
            //}
        }
        public static void DeleteDanjuTable(Expression<Func<DanjuTable, bool>> func)
         {           
            var list = GetDanjuTablelst(func);
            foreach (var l in list)
            {
                RemainMoneyTableService.DeleteRemainMoneyTable(x=>x.ReceiptNum==l.dh );
            }
            Connect.dbcontext.Deleteable<DanjuTable>().Where(func).ExecuteCommand ();
         }
        public static void DeleteDanjuTable(DanjuTable danju)
        {
            RemainMoneyTableService.DeleteRemainMoneyTable(x => x.ReceiptNum == danju.dh);
            Connect.dbcontext.Deleteable(danju).ExecuteCommand();
        }
        public static void DeleteDanjuTable(List<DanjuTable> list)
        {
            Connect.dbcontext.Deleteable<RemainMoneyTable >().In(list.Select(x=>x.dh).ToList()).ExecuteCommand ();
            Connect.dbcontext.Deleteable(list).ExecuteCommand();
        }
    }
}
